Fix ObjectDisposedException in BuildsWhileBuildIsRunningOnServer test#13316
Merged
rainersigwald merged 5 commits intomainfrom Mar 3, 2026
Merged
Fix ObjectDisposedException in BuildsWhileBuildIsRunningOnServer test#13316rainersigwald merged 5 commits intomainfrom
rainersigwald merged 5 commits intomainfrom
Conversation
Contributor
|
Hello @@copilot, I noticed that you’re changing an .swr file or any file under src/Package/MSBuild.VSSetup.. Please make sure to validate this change by an experimental VS insertion. This is accomplished by pushing to an exp/* branch, which requires write permissions to this repo. |
…ildsWhileBuildIsRunningOnServer Co-authored-by: rainersigwald <3347530+rainersigwald@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] Fix disposal order for watcher and mre in tests
Fix ObjectDisposedException in BuildsWhileBuildIsRunningOnServer test
Mar 2, 2026
…e reordering Co-authored-by: rainersigwald <3347530+rainersigwald@users.noreply.github.com>
Contributor
There was a problem hiding this comment.
Pull request overview
Fixes an ObjectDisposedException crash in the BuildsWhileBuildIsRunningOnServer unit test by ensuring FileSystemWatcher is disposed before the ManualResetEvent it signals, preventing late callbacks from using a disposed handle.
Changes:
- Reordered
mreandFileSystemWatcherusingdeclarations sowatcheris disposed first. - Added an explanatory comment to prevent accidental reordering in the future.
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
JanProvaznik
approved these changes
Mar 3, 2026
This was referenced Mar 3, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
FileSystemWatchercallbacks could callmre.Set()aftermrewas already disposed, crashing the test host withObjectDisposedException. Withusing var, disposal happens in reverse declaration order —watcherwas declared first, somrewas disposed beforewatcherstopped delivering events.Changes Made
mreandwatcherinBuildsWhileBuildIsRunningOnServersowatcheris disposed first (silencing callbacks) beforemreis disposed:Testing
Existing test
BuildsWhileBuildIsRunningOnServercovers the scenario. No new tests needed — this is a correctness fix for the test itself.Notes
No try/catch or manual
EnableRaisingEvents = falseneeded; correct disposal ordering is sufficient.Original prompt
Should the declaration order between
[Chronological Review: - User reported a CI crash in Azure DevOps build `#13175` (`buildId=1316368`) with an unhandled `System.ObjectDisposedException` in `MSBuildServer_Tests.cs:line 198` during `FileSystemWatcher` callback execution. - User repeated the same request after a model switch notice, asking how to harden the test and fix the root cause. - Agent inspected `src/MSBuild.UnitTests/MSBuildServer_Tests.cs` and identified disposal-order race in `BuildsWhileBuildIsRunningOnServer`: `using var watcher` declared before `using ManualResetEvent mre`; reverse disposal means `mre` can be disposed before late watcher callbacks run. - Agent edited the test to catch `ObjectDisposedException` around `mre.Set()` and added `watcher.EnableRaisingEvents = false;` before cleanup. - Agent verified updated method lines 176–240. - User then requested: “put up a draft PR”. - Agent began git workflow (status/branch checks, then branch+commit command), but the final branch/commit tool execution was interrupted before completion.]mreandwatcherbe changed? If so does that mean the other changes aren't necessary?[Intent Mapping:
[Technical Inventory:
[Fact],[Theory]), assertions via Shouldly.System.IO.FileSystemWatcher,ManualResetEvent, asynchronousTask.Run, process management (Process.GetProcessById,KillTree), MSBuild test infrastructure (TestEnvironment,RunnerUtilities.ExecMSBuild).FSEventsasynchronous/late callbacks.[Code Archaeology:
Q:\src\msbuild\src\MSBuild.UnitTests\MSBuildServer_Tests.cs.BuildsWhileBuildIsRunningOnServer()(starts around line 177).Createdhandler directly calledmre.Set();(line 198 in stack trace context).mre.Set()intry { ... } catch (ObjectDisposedException) { ... }with comment explaining benign late/duplicate macOS callbacks.watcher.EnableRaisingEvents = false;before_env.Dispose();in cleanup to reduce callback race window.1 file changed, 11 insertions(+), 1 deletion(-).][Progress Assessment:
[Context Validation:
[Recent Commands Analysis:
functions.report_intentwith intent"Creating draft PR".functions.powershellcommand:cd Q:\src\msbuild && git --no-pager diff --stat && echo "---" && git --no-pager branch --show-currentsrc/MSBuild.UnitTests/MSBuildServer_Tests.cs | 12 +++++++++++-,1 file changed, 11 insertions(+), 1 deletion(-), exit code 0; branch name not shown (likely detached HEAD).functions.powershellcommand:cd Q:\src\msbuild && git --no-pager branch --show-current && git --no-pager log --oneline -1ac211928a0 (HEAD, origin/main, origin/HEAD) Only get command line args names on modern .NET (#13314); exit code 0.functions.powershellcommand attempting branch+commit:git checkout -b fix/server-test-disposed-handle && git add ... && git commit -m "Fix ObjectDisposedException crash ... Fixes #13175 ...""The execution of this tool, or a previous tool was interrupted."Created from Copilot CLI via the copilot delegate command.
✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.